/**
 * 河北省政府采购网爬虫
 *  
 */
import { Base, Item } from '../base';
class hbzfcg extends Base {
    flag: number=0;
    constructor() {
        super();
        this.name = '河北省政府采购网';
        this.enabled = true;
        this.encode = "utf-8";
        this.debug = false;
        this.timeout = 1000000;
        this.collection = "ztb";
        this.collectionShowName = "招投标信息";
        this.keyField = "title";
    }
    getItems(pageIndex: number): Item[] {
        let num = pageIndex + 1;
        const url = `http://www.ccgp-hebei.gov.cn/zfcg/web/getBidingList_${num}.html`;
        let pageCount = this.getPageCount();
        let content = this.spider.getSync(url);
        let $ = this.cheerio.load(content);
        let result: Item[] = [];
        let that = this;
        try {
            $('#moredingannctable tr').each(function (index, ele) {
                if (that.flag % 2 == 0) {
                    let item = that.buildItem();
                    (<any>item).title = $(ele).find('td').eq(1).text().trim();
                    item.source = that.name;
                    let urll = $(ele).attr('onclick').substring(14, 19);
                    let type = $(ele).attr('onclick').substring(23, 24);
                    let uri = `http://www.ccgp-hebei.gov.cn/zfcg/${type}/bidingAnncDetail_${urll}.html`;
                    (<any>item).url = `http://www.ccgp-hebei.gov.cn/zfcg/${type}/bidingAnncDetail_${urll}.html`;
                    let nextObject = $(ele).next();
                    if (nextObject)
                    (<any>item)['发布时间'] = nextObject.find('td').eq(1).find('span').eq(0).text();
                    (<any>item)['地域'] = nextObject.find('td').eq(1).find('span').eq(1).text();
                    (<any>item)['采购人'] = nextObject.find('td').eq(1).find('span').eq(2).text();
                    result.push(item);
                    result = that.getExtraItem(uri, item, result);
                }
                that.flag++;
            });
            //(<any>item)['采购时间']=$(ele).find('td').eq(1).find('a').text().substring(1,10);

        } catch (error) {
            console.error(error);
        }
        return result;
    }
    getExtraItem(url: string, item: Item, result: Item[]): Item[] {
        let content = this.spider.getSync(url);
        let $ = this.cheerio.load(content);
        (<any>item).content = $('table').eq(2).html();
        result.push(item);
        return result;
    }
    getPageCount(): number {
        return 20;
    }
}
module.exports = new hbzfcg();